package com.rainwood.material.ui;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;

import com.alibaba.fastjson.JSON;
import com.rainwood.material.R;
import com.rainwood.material.base.BaseActivity;
import com.rainwood.material.bean.ScanMaterBean;
import com.rainwood.material.net.NetBean;
import com.rainwood.material.view.titlebar.OnTitleBarListener;
import com.rainwood.material.view.titlebar.TitleBar;

import java.util.HashMap;
import java.util.Map;

/**
 * @auther : hhx
 * @date :  2021/3/5
 * @des :
 */
public class CheckInDetailActivity extends BaseActivity {
    private TitleBar titleBar;
    private Button btnSubmit;

    private NestedScrollView netView;
    private TextView tvMaterialType;
    private TextView tvOneType;
    private TextView tvTwoType;
    private TextView tvMaterCode;
    private TextView tvMaterName;
    private TextView tvMaterModel;
    private TextView tvOutRemark;
    private TextView tvOutCount;
    private EditText etMaterialCount;
    private EditText etRemark;
    private RelativeLayout rlLayout;


    private ScanMaterBean scanMaterBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_check_in_detail;
    }

    @Override
    protected void initView() {
        titleBar = (TitleBar) findViewById(R.id.title_bar);
        btnSubmit = (Button) findViewById(R.id.btn_submit);



        titleBar = (TitleBar) findViewById(R.id.title_bar);
        netView = (NestedScrollView) findViewById(R.id.net_view);
        tvMaterialType = (TextView) findViewById(R.id.tv_material_type);
        tvOneType = (TextView) findViewById(R.id.tv_one_type);
        tvTwoType = (TextView) findViewById(R.id.tv_two_type);
        tvMaterCode = (TextView) findViewById(R.id.tv_mater_code);
        tvMaterName = (TextView) findViewById(R.id.tv_mater_name);
        tvMaterModel = (TextView) findViewById(R.id.tv_mater_model);
        tvOutRemark = (TextView) findViewById(R.id.tv_out_remark);
        tvOutCount = (TextView) findViewById(R.id.tv_out_count);
        etMaterialCount = (EditText) findViewById(R.id.et_material_count);
        etRemark = (EditText) findViewById(R.id.et_remark);
        rlLayout = (RelativeLayout) findViewById(R.id.rl_layout);
        btnSubmit = (Button) findViewById(R.id.btn_submit);


        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });

        btnSubmit.setOnClickListener(v -> {
            turnToSuccessPage();
        });


    }

    private void turnToSuccessPage() {
        startActivity(new Intent(this,OperateSuccessActivity.class));
    }

    @Override
    protected void initData() {
        String code = getIntent().getStringExtra("code");
        getMaterialDetail(code);
    }

    private void getMaterialDetail(String code) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("no", code);
        addPostRequest(NetBean.MATERIALDETIAL, map);
    }

    @Override
    public void onSuccess(String res ,int tag) {
        if (tag == NetBean.MATERIALDETIAL.getTag()) {
            scanMaterBean = JSON.parseObject(res, ScanMaterBean.class);
            if (scanMaterBean != null) {
                fillTopView(scanMaterBean);
            }
        }
    }

    private void fillTopView(ScanMaterBean scanMaterBean) {

        tvMaterialType.setText(scanMaterBean.getMaterialType());
        tvOneType.setText(scanMaterBean.getFirstClassification());
        tvTwoType.setText(scanMaterBean.getSecondClassification());
        tvMaterCode.setText(scanMaterBean.getNo());
        tvMaterName.setText(scanMaterBean.getName());
        tvMaterModel.setText(scanMaterBean.getSize());
        tvOutRemark.setText(scanMaterBean.getRemarks());

    }

    @Override
    public void onFail(String res, int tag) {

    }

    @Override
    public void onError(String msg) {

    }
}
